﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Catering_Rules;
using Catering.View_Operaction;

namespace Catering.UCPanle
{
    public partial class UCSearchToolForRCJK : System.Web.UI.UserControl
    {
        #region 属性
        private int[] RawId;//食品大类ID
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                loadData();
                for (int i = 0; i < CheckBoxList1.Items.Count; i++)
                {
                    CheckBoxList1.Items[i].Selected = true;
                }
            }
        }

        /// <summary>
        /// 获取选中大类
        /// </summary>
        /// <returns></returns>
        public int[] GetRaw()
        {
            int selectNum1 = 0;
            for (int i = 0; i < CheckBoxList1.Items.Count; i++)
            {
                if (CheckBoxList1.Items[i].Selected)
                {
                    selectNum1 += 1;
                }
            }
            RawId = new int[selectNum1];
            int numTemp = 0;
            for (int i = 1; i < CheckBoxList1.Items.Count; i++)
            {
                if (CheckBoxList1.Items[i].Selected)
                {
                    RawId[numTemp] = Int32.Parse(CheckBoxList1.Items[i].Value);
                    numTemp += 1;
                }
            }
            return RawId;
        }

        private void loadData()
        {
            List<Catering_Model.RawMaterialsType> listRawType = new Catering_Rules.RawMaterialsType().GetModelList("").Where(p => p.id != 1 && p.id != 2 && p.id != 9 && p.id != 10 && p.id != 11 && p.id != 5 && p.id != 12).ToList<Catering_Model.RawMaterialsType>();
            if (CheckBoxList1 != null && CheckBoxList1.Items.Count > 0)
            {
                CheckBoxList1.Items.Clear();
            }
            CheckBoxList1.Items.Add(new ListItem("全部", "-1"));
            foreach (Catering_Model.RawMaterialsType itemRawType in listRawType)
            {
                CheckBoxList1.Items.Add(new ListItem(itemRawType.Name, itemRawType.id.ToString()));
            }
        }

        protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (CheckBoxList1.SelectedItem != null)
            {
                if (CheckBoxList1.SelectedItem.Text.Equals("全部"))
                {
                    if (CheckBoxList1.SelectedItem.Selected)
                    {
                        for (int i = 0; i < CheckBoxList1.Items.Count; i++)
                        {
                            CheckBoxList1.Items[i].Selected = true;
                        }
                    }
                }
                else
                {
                    int num = 0;
                    for (int i = 1; i < CheckBoxList1.Items.Count; i++)
                    {
                        if (CheckBoxList1.Items[i].Selected == true)
                        {
                            num = num + 1;
                        }
                    }
                    if (num == (CheckBoxList1.Items.Count - 1))
                    {
                        for (int i = 0; i < CheckBoxList1.Items.Count; i++)
                        {
                            CheckBoxList1.Items[i].Selected = false;
                        }
                    }
                }
            }
        }

        protected void UpdatePanel1_DataBinding(object sender, EventArgs e)
        {
        }
    }
}